
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _utils = require('../utils');

const _popup = require('../mixins/popup');

const _popup2 = _interopRequireDefault(require('../popup'));

// Utils
// Mixins
// Components
const PRESET_ICONS = ['qq', 'weibo', 'wechat', 'link', 'qrcode', 'poster'];

const _createNamespace = (0, _utils.createNamespace)('share-sheet');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];
const t = _createNamespace[2];

const _default2 = createComponent({
  props: (0, _extends2.default)({}, _popup.popupMixinProps, {
    title: String,
    cancelText: String,
    description: String,
    getContainer: [String, Function],
    options: {
      type: Array,
      default: function _default() {
        return [];
      },
    },
    overlay: {
      type: Boolean,
      default: true,
    },
    closeOnPopstate: {
      type: Boolean,
      default: true,
    },
    safeAreaInsetBottom: {
      type: Boolean,
      default: true,
    },
    closeOnClickOverlay: {
      type: Boolean,
      default: true,
    },
  }),
  methods: {
    onCancel: function onCancel() {
      this.toggle(false);
      this.$emit('cancel');
    },
    onSelect: function onSelect(option, index) {
      this.$emit('select', option, index);
    },
    toggle: function toggle(val) {
      this.$emit('input', val);
    },
    getIconURL: function getIconURL(icon) {
      if (PRESET_ICONS.indexOf(icon) !== -1) {
        return `https://img.yzcdn.cn/vant/share-icon-${icon}.png`;
      }

      return icon;
    },
    genHeader: function genHeader() {
      const h = this.$createElement;
      const title = this.slots('title') || this.title;
      const description = this.slots('description') || this.description;

      if (!title && !description) {
        return;
      }

      return h('div', {
        class: bem('header'),
      }, [title && h('h2', {
        class: bem('title'),
      }, [title]), description && h('span', {
        class: bem('description'),
      }, [description])]);
    },
    genOptions: function genOptions(options, showBorder) {
      const _this = this;

      const h = this.$createElement;
      return h('div', {
        class: bem('options', {
          border: showBorder,
        }),
      }, [options.map((option, index) => h('div', {
        class: bem('option'),
        on: {
          click: function click() {
            _this.onSelect(option, index);
          },
        },
      }, [h('img', {
        attrs: {
          src: _this.getIconURL(option.icon),
        },
        class: bem('icon'),
      }), option.name && h('span', {
        class: bem('name'),
      }, [option.name])]))]);
    },
    genRows: function genRows() {
      const _this2 = this;

      const { options } = this;

      if (Array.isArray(options[0])) {
        return options.map((item, index) => _this2.genOptions(item, index !== 0));
      }

      return this.genOptions(options);
    },
    genCancelText: function genCancelText() {
      const h = this.$createElement;
      const cancelText = (0, _utils.isDef)(this.cancelText) ? this.cancelText : t('cancel');

      if (cancelText) {
        return h('button', {
          attrs: {
            type: 'button',
          },
          class: bem('cancel'),
          on: {
            click: this.onCancel,
          },
        }, [cancelText]);
      }
    },
  },
  render: function render() {
    const h = arguments[0];
    return h(_popup2.default, {
      attrs: {
        round: true,
        value: this.value,
        position: 'bottom',
        overlay: this.overlay,
        duration: this.duration,
        lazyRender: this.lazyRender,
        lockScroll: this.lockScroll,
        getContainer: this.getContainer,
        closeOnPopstate: this.closeOnPopstate,
        closeOnClickOverlay: this.closeOnClickOverlay,
        safeAreaInsetBottom: this.safeAreaInsetBottom,
      },
      class: bem(),
      on: {
        input: this.toggle,
      },
    }, [this.genHeader(), this.genRows(), this.genCancelText()]);
  },
});

exports.default = _default2;
